//
//  MBDDetailCell.swift
//  MasterBoy
//
//  Created by kyson on 2023/3/28.
//

import Foundation

import UIKit
import YYText
import SnapKit

// 定义枚举
enum MBDDetailType {
    case text
    case image
}

class MBDDetailAvatarCell: MBDCommonCell {

    private var titleLabel : YYLabel!
    private var modifyLabel : YYLabel!
    private var modifyImageView : UIImageView!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        self.initialize()
    }
    
    override var cellModel: Any! {
        didSet {
            let cellModel = self.cellModel as! MBDDetailCellModel
            self.title = cellModel.itemName
            
            self.avatarTobeModify = cellModel.detailImageURLString

            self.accessable = cellModel.accessable
            self.location = cellModel.location
            
        }
    }
    
    func initialize() {
        self.contentView.setDynamicForgroundColor()
        self.commonBackgroundView.setDynamicForgroundColor()
        
        titleLabel = YYLabel()
        self.commonBackgroundView.addSubview(titleLabel)
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(self.commonBackgroundView)
            make.left.equalTo(commonBackgroundView).offset(8)
        }
        
        modifyImageView = UIImageView()
        modifyImageView.layer.cornerRadius = 4
        modifyImageView.clipsToBounds = true
        self.commonBackgroundView.addSubview(modifyImageView)
        
        modifyImageView.snp.makeConstraints { make in
            make.centerY.equalTo(self.commonBackgroundView)
            make.right.equalTo(self.rightArrowImage.snp.left).offset(-10)
            make.size.equalTo(CGSizeMake(40, 40))
        }
    }
    
    
    var title : String? {
        didSet {
            let usernameAttributeString = NSMutableAttributedString(string: self.title ?? "")
            usernameAttributeString.yy_font = UIFont.systemFont(ofSize: 14)
            usernameAttributeString.setDynamicTextColor()

            titleLabel.attributedText = usernameAttributeString
        }
    }
    
    var avatarTobeModify : String? {
        didSet {
            
            let avatar = self.avatarTobeModify ?? ""
            if avatar.hasPrefix(MBDPath2URLUtils.filePathPrefix) {
                let imageName = avatar.replacingOccurrences(of: MBDPath2URLUtils.filePathPrefix, with: "")
                
                modifyImageView.image = UIImage(named: imageName)
            } else {
                let url = URL.init(string: MBDPath2URLUtils.wrapAvatarURL(path: avatar))
                modifyImageView.kf.setImage(with: url)
            }
        }
    }
}


class MBDDetailUserNameCell: MBDCommonCell {

    private var titleLabel : YYLabel!
    private var modifyLabel : YYLabel!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        self.initialize()
    }
    
    override var cellModel: Any! {
        didSet {
            let cellModel = self.cellModel as! MBDDetailCellModel
            self.title = cellModel.itemName
            self.detailTobeModify = cellModel.detailText
            
            self.accessable = cellModel.accessable
            self.location = cellModel.location
            
            self.authed = cellModel.authed
            
        }
    }
    
    func initialize() {
        self.contentView.setDynamicForgroundColor()
        self.commonBackgroundView.setDynamicForgroundColor()
        
        titleLabel = YYLabel()
        self.commonBackgroundView.addSubview(titleLabel)
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(self.commonBackgroundView)
            make.left.equalTo(commonBackgroundView).offset(8)
        }
        
        modifyLabel = YYLabel()
        self.commonBackgroundView.addSubview(modifyLabel)
        
        modifyLabel.snp.makeConstraints { make in
            make.centerY.equalTo(self.commonBackgroundView)
            make.right.equalTo(self.rightArrowImage.snp.left).offset(-10)
        }
    }
    
    
    var title : String? {
        didSet {
            
            let usernameAttributeString = NSMutableAttributedString(string: self.title ?? "")
            usernameAttributeString.yy_font = UIFont.systemFont(ofSize: 14)
            usernameAttributeString.setDynamicTextColor()
            
            if self.authed == true {
                let authenticationImage = UIImage(named: "ico_userinfo_authentication")!
                let authImageString = NSAttributedString.yy_attachmentString(withContent: authenticationImage, contentMode: .scaleAspectFit, attachmentSize: authenticationImage.size, alignTo: UIFont.systemFont(ofSize: 14), alignment: .center)
                
                usernameAttributeString.yy_appendString("  ")
                usernameAttributeString.append(authImageString)
            }
            
            titleLabel.attributedText = usernameAttributeString
        }
    }
    
    var detailTobeModify : String? {
        didSet {
            let modifyDetail = self.detailTobeModify ?? ""
            let usernameAttributeString = NSMutableAttributedString(string:  modifyDetail)
            usernameAttributeString.yy_font = UIFont.systemFont(ofSize: 14)
            usernameAttributeString.setDynamicTextColor()
            
            modifyLabel.attributedText = usernameAttributeString
        }

    }
    
    var authed : Bool? {
        didSet {
            let cellModel = self.cellModel as! MBDDetailCellModel
            self.title = cellModel.itemName

        }
    }
}
